#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "user_login_widget.h"

#include "net/tcp_client.h"
#include "global_config.h"
#include "user.h"

#include "user_start_matching.pb.h"
#include "user_stop_matching.pb.h"

#include <QMouseEvent>
#include <QVBoxLayout>

MainWindow::MainWindow(QWidget *parent, std::shared_ptr<tcp_client> client) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    mineSweeperWgt_(std::make_shared<MineSweeprWidget>()),
    userInfoWgtPtr_(std::make_shared<UserInfoWidget>(this)),
    clientPtr_(client),
    lcdNumber_(this),
    matchingButton_(),
    timer_(this)
{
    ui->setupUi(this);

    // 添加到主窗口的中心控件
    // QWidget *centralWidget = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(&matchingButton_);
    // setCentralWidget(centralWidget);

    mineSweeperWgt_->setTcpClient(client);
    mineSweeperWgt_->show();

    userInfoWgtPtr_->move(mineSweeperWgt_->colNum() * gMineSweeperCellPixSize, 0);
    auto &user = User::getInstance();

    userInfoWgtPtr_->setUserName(QString::fromStdString(user.username()));
    userInfoWgtPtr_->setUid(user.uid());
    userInfoWgtPtr_->setUserStatus(user.userStatus());
    userInfoWgtPtr_->show();

    matchingButton_.setText("开始匹配");
    matchingButton_.move(mineSweeperWgt_->colNum() * gMineSweeperCellPixSize, 110);
    matchingButton_.show();

    lcdNumber_.move(mineSweeperWgt_->colNum() * gMineSweeperCellPixSize, 70);
    lcdNumber_.hide();

    
    /* 开始匹配按钮 */
    connect(&matchingButton_, &QPushButton::clicked, this, [this]() {
        User &user = User::getInstance();
        if (user.userStatus() == User::GAMEING) {
            std::cout << "玩家正在游戏中，无法参与或退出匹配" << std::endl;
            return;
        }

        if (is_matching_) {
            UserStopMatcingC2SProto user_stop_matching_c2s_proto;
            user_stop_matching_c2s_proto.set_uid(User::getInstance().uid());

            network_message msg(MSG_TYPE_C2S_STOP_MATCHING, user_stop_matching_c2s_proto.SerializeAsString());
            clientPtr_->send_message(msg.to_string());
        } else {
            UserStartMatchingC2SProto user_start_matching_c2s_proto;
            user_start_matching_c2s_proto.set_uid(User::getInstance().uid());

            network_message msg(MSG_TYPE_C2S_START_MATCHING, user_start_matching_c2s_proto.SerializeAsString());
            clientPtr_->send_message(msg.to_string());

            matchingButton_.setText("匹配中，点击停止匹配");
            is_matching_ = true;
        }
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

// void MainWindow::mousePressEvent(QMouseEvent* event)
// {
//     int x = event->pos().x();
//     int y = event->pos().y();

//     /* 鼠标点击的坐标位于游戏网格区域内，将鼠标点击信号转发到mineSweeperWgt_处理 */
//     if ((x >= 0) &&
//         (x < gMineSweeperCellPixSize * mineSweeperWgt_->colNum()) &&
//         (y >= 0) &&
//         (y <gMineSweeperCellPixSize * mineSweeperWgt_->rowNum())) {
//         return mineSweeperWgt_->mousePressEvent(event);
//     }
// }
